import matplotlib.pyplot as plt
import numpy as np

# 读取多项式信息
filename = 'resultE.txt'
f = open(filename, 'r')
line = f.readlines()
f.close()

col = ["blue", "red"]
# 按照不同日期时间段绘制曲线图
for day in [29, 44]:
    plt.figure()
    x = np.linspace(-1, day, 1000)
    for i in [1, 6]:
        # 绘制不同例子对应的曲线图
        k = (i - 1) // 5
        coff = [float(s) for s in line[i].split()]
        n = len(coff)
        y = np.zeros(1000, dtype=float)
        # 计算各点多项式值
        for j in range(n - 1, -1, -1):
            y = coff[j] + np.multiply(y, x)
        s1 = "y" + str(k + 1) + "=f(x)"
        plt.plot(x, y, col[k - 1], label=s1)

    # 绘制水平座标轴
    z = np.zeros(1000)
    plt.plot(x, z, "black", label="y=0")

    # 图例和注记
    plt.title("E. Two Samples and days - " + str(day) + " days")
    plt.xlabel("Day")
    plt.ylabel("Weight")
    plt.legend()

    # 图片保存至指定文件夹
    figurename = "../report/figure/E_Tree" + str(day) + "days.png"
    plt.savefig(figurename)

print("Finish draw figure E.")